package com.skwen.android_room.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.skwen.android_room.R;
import com.skwen.android_room.bean.Books;
import com.skwen.android_room.databinding.MainListItemBinding;

import java.util.List;

public class BooksAdapter extends RecyclerView.Adapter<CommonHolder> {

    private List<Books> mList;

    private AdapterClickListener mAdapterClickListener;

    @NonNull
    @Override
    public CommonHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CommonHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.main_list_item, parent, false).getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull CommonHolder holder, final int position) {
        MainListItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        if (binding != null) {
            binding.setItem(mList.get(position));
            binding.executePendingBindings();
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mAdapterClickListener.onItemClick(position, mList.get(position));
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return mAdapterClickListener.onItemLongClick(position, mList.get(position));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setData(final List<Books> list) {
        if (mList == null) {
            mList = list;
            notifyItemRangeChanged(0, mList.size());
        } else {
            DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
                @Override
                public int getOldListSize() {
                    return mList.size();
                }

                @Override
                public int getNewListSize() {
                    return list.size();
                }

                @Override
                public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                    return mList.get(oldItemPosition).getId().equals(list.get(newItemPosition).getId());
                }

                @Override
                public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                    //懒 不想写其他得比较了 对于这个对象 id用来测试够用了
                    return mList.get(oldItemPosition).getId().equals(list.get(newItemPosition).getId());
                }
            });
            result.dispatchUpdatesTo(this);
        }
    }

    public void addClickListener(AdapterClickListener clickListener) {
        mAdapterClickListener = clickListener;
    }

}
